package com.example.it.looker.view.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;

import com.example.it.looker.R;
import com.example.it.looker.model.datasource.NewsDataSource;
import com.example.it.looker.model.entity.News;
import com.example.it.looker.presenter.NewsPresenter;
import com.example.it.looker.presenter.contact.NewsContact;
import com.example.it.looker.utils.L;
import com.example.it.looker.view.BaseFragment;
import com.example.it.looker.view.adapter.NewsListAdapter;
import com.example.it.looker.view.adapter.NewsPagerAdapter;
import com.example.it.looker.view.customview.LoopViewPager;
import com.example.it.looker.view.customview.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 鲁迅认识的那只猹 on 8/31/2017 7:21 PM.
 * Emial 1258730808@qq.com
 * Desc:
 */

public class NewsFragment extends BaseFragment implements NewsContact.NewsView, MyListView.OnAppendElement {

    private MyListView newListView;
    private ProgressDialog progressDialog;

    private NewsContact.NewsPresenter newsPresenter;
    private NewsListAdapter newsListAdapter;

    @Override
    public View bindingView(LayoutInflater inflater, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_news, null);
    }

    @Override
    public void initFindView(View view) {
        newListView = (MyListView) view.findViewById(R.id.lvNews);
    }

    @Override
    public void init() {
        //初始化View和Model之间的纽带
        this.newsPresenter = new NewsPresenter(this, NewsDataSource.getInstance());

        newsListAdapter = new NewsListAdapter(this.getContext(), new ArrayList<News>());
        this.newListView.setAdapter(newsListAdapter);
        //加载数据
        newsPresenter.loadData();

    }

    @Override
    public void registerEvent() {
        this.newListView.setOnAppendElement(this);
    }

    /**
     * 生命周期方法，用来回收被Presenter占用的View
     */
    @Override
    public void onDestroyView() {
        this.newsPresenter.onDestroy();
    }

    /**
     * 加载新闻列表
     */
    @Override
    public void loaded(final List<News> list) {
        this.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {

                newsListAdapter.addRange(-1, list);
                L.i(newsListAdapter.getCount() + "");

                if (progressDialog.isShowing())
                    progressDialog.dismiss();
                newsListAdapter.notifyDataSetChanged();
            }
        });


    }

    /**
     * 加载热门新闻,这里是屏幕头部的ViewPager
     */
    @Override
    public void topNewsLoaded(final List<News> newsList) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                View viwe = LayoutInflater.from(getContext()).inflate(R.layout.ctl_viewpager, newListView, false);
                LoopViewPager viewPager = (LoopViewPager) viwe.findViewById(R.id.ctl_viewpager);
                PagerAdapter adapter = new NewsPagerAdapter(getContext(), newsList, R.layout.page_news);
                viewPager.setAdapter(adapter);
                newListView.addHeaderView(viwe);
                viewPager.setCurrentItem(1);
            }
        });
    }

    @Override
    public void loadedMore(final List<News> newsList) {
        try {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    newsListAdapter.addRange(-1, newsList);
                    newsListAdapter.notifyDataSetChanged();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            L.i("NewsFragment.loadedMore Error");
        }
    }

    /**
     * 显示正在加载的Dialog
     */
    @Override
    public void loading() {
        progressDialog = new ProgressDialog(this.getContext());
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setTitle("正在玩命加载~");
        progressDialog.show();
    }

    @Override
    public void appendElement() {
        this.newsPresenter.loadMore();
    }
}
